﻿
//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
#include "ace.ReferenceObject.h"

//----------------------------------------------------------------------------------
//
//----------------------------------------------------------------------------------
namespace ace {
	//----------------------------------------------------------------------------------
	// extern経由で他から参照される部分
	//----------------------------------------------------------------------------------
	int32_t					g_globalRefCount__ = 0;

	//----------------------------------------------------------------------------------
	//
	//----------------------------------------------------------------------------------
	ReferenceObject::ReferenceObject()
		: m_reference(1)
	{
		g_globalRefCount__++;
	}

	//----------------------------------------------------------------------------------
	//
	//----------------------------------------------------------------------------------
	ReferenceObject::~ReferenceObject()
	{}

	//----------------------------------------------------------------------------------
	//
	//----------------------------------------------------------------------------------
	int ReferenceObject::AddRef()
	{
		m_reference++;
		g_globalRefCount__++;
		return m_reference;
	}

	//----------------------------------------------------------------------------------
	//
	//----------------------------------------------------------------------------------
	int ReferenceObject::GetRef()
	{
		return m_reference;
	}

	//----------------------------------------------------------------------------------
	//
	//----------------------------------------------------------------------------------
	int ReferenceObject::Release()
	{
		assert(m_reference > 0);
		m_reference--;
		g_globalRefCount__--;
		if (m_reference == 0)
		{
			delete this;
			return 0;
		}
		return m_reference;
	}

	//----------------------------------------------------------------------------------
	//
	//----------------------------------------------------------------------------------
};